<?php
namespace app\adm\controller;
use think\Controller;
use think\Db;
use think\Request;

/**
 * 系统管理
 */
class System extends Index{
	public function __construct(Request $request){
		parent::__construct($request);
	}

	/*
	* 菜单页
	*/
    public function nav(Request $request){
    	$data = Db::name('auth_nav')->order('order_number is null,order_number,id')->select();

		$list = tree($data,'tree','name');unset($data);

		$this->assign('list',$list);
		return $this->fetch($request->action(),['title'=>'菜单管理']);
    }

    /*
	* 添加菜单
    */
    public function add_nav(Request $request){
    	if($request->isAJax()){
			$data = input('post.');
			$datas = [
				'mca'	=> $data['addMca'],
				'name'	=> $data['addName'],
				'pid'	=> $data['pid'],
				'ico'	=> $data['addIco'],
			];
			$res = Db::name('auth_nav')->insert($datas);
			if($res){
				adminOperationRecord('添加了左侧菜单，名称为：'.$datas['name']);
				$this->success('添加成功！');
			}else{
				$this->error('添加失败！');
			}
		}
    }

    /*
	* 删除菜单（包括子菜单）
	*/
	public function delete_nav(Request $request){
		if($request->isAJax()){
			$id = input('post.id');

			$count = Db::name('auth_nav')->where(['pid'=>$id])->count();

            if($count >= 1){
            	$this->error('尚有子菜单，不能删除！');
            }
            $name = Db::name('auth_nav')->where(['id'=>$id])->value('name');
            $res = Db::name('auth_nav')->where(['id'=>$id])->delete();
            if($res){
            	adminOperationRecord('删除了一条左侧菜单，名称为：'.$name);
                $this->success();
            }else{
                $this->error('删除失败');
            }
		}
	}

	/*
	* 菜单修改
	*/
	public function update_nav(Request $request){
		if($request->isAJax()){
			$data = input('post.');
			$data1 = ['name' =>$data['name'],'mca' =>$data['mca'],'ico' =>$data['ico']];
			$res = Db::name('auth_nav')->where(['id'=>$data['id']])->update($data1);
			if($res){
				adminOperationRecord('修改了左侧菜单，修改完为：'.$data1['name']);
				$this->success('修改成功！');
			}else{
				$this->error('修改失败！');
			}
		}
	}
}
